package lambdademo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—24
 * Time: 9:23
 */

    //无返回值无参数
    @FunctionalInterface
    interface NoParameterNoReturn {
        void test();
    }
    //无返回值一个参数
    @FunctionalInterface
    interface OneParameterNoReturn {
        void test(int a);
    }
    //无返回值多个参数
    @FunctionalInterface
    interface MoreParameterNoReturn {
        void test(int a,int b);
    }
    //有返回值无参数
    @FunctionalInterface
    interface NoParameterReturn {
        int test();
    }
    //有返回值一个参数
    @FunctionalInterface
    interface OneParameterReturn {
        int test(int a);
    }
    //有返回值多参数
    @FunctionalInterface
    interface MoreParameterReturn {
        int test(int a,int b);
    }
    class Demo {
        void fun() {
            System.out.println("dasd");

        }
    }
public class Demo02 {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "bit");
        map.put(3, "hello");
        map.put(4, "lambda");

        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.println("key:"+integer+"val:"+s);
            }
        });
        map.forEach((x,y)-> System.out.println("key:"+x+"val:"+y));
    }
    public static void main8(String[] args) {
        //Lambda内部也是匿名内部类，用外部变量不能被修改
        ArrayList<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("bit");
        list.add("hellol");
        list.add("lambda");
        //打印全部
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        list.forEach(s -> System.out.println(s));
        //排序
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        list.sort(((o1, o2) -> o1.compareTo(o2)));
        list.forEach(s -> System.out.println(s));
    }
    public static void main7(String[] args) {
        int a = 10;
        //在匿名内部类中，变量捕获（外部变量的捕获），看使用的外部变量一定是未被修改过的
        new Demo() {
            @Override
            void fun() {
                int c = 99;
                c = 100;
                System.out.println(c+"重写一下" + a);
            }
        }.fun();
    }
    public static void main6(String[] args) {
        MoreParameterReturn moreParameterReturn = (x,y) -> x+y;
        System.out.println(moreParameterReturn.test(10, 20));
    }
    public static void main5(String[] args) {
        OneParameterReturn oneParameterReturn = x -> 2*x;
        System.out.println(oneParameterReturn.test(10));

    }
    public static void main4(String[] args) {
        NoParameterReturn noParameterReturn = () -> {return 10;};
        int ret = noParameterReturn.test();
        System.out.println(ret);

        NoParameterReturn noParameterReturn1 = () ->10;
        int ret2 = noParameterReturn1.test();
        System.out.println(ret2);
    }
    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn = (int a,int b) -> {
            System.out.println(a+b);
        };
        moreParameterNoReturn.test(10,20);
        //要求两个类型同时省略
        MoreParameterNoReturn moreParameterNoReturn1 = (a,b) -> System.out.println(a+b);
        moreParameterNoReturn1.test(20,30);
    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = (int x) -> {
            System.out.println(x);
        };
        oneParameterNoReturn.test(100);
        System.out.println("简化：");
        OneParameterNoReturn oneParameterNoReturn1 = x -> System.out.println(x);
        OneParameterNoReturn oneParameterNoReturn2 = System.out::println;
        oneParameterNoReturn1.test(10);
    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("测试一下");
            }
        };
        noParameterNoReturn.test();

        NoParameterNoReturn noParameterNoReturn1 = () -> {System.out.println("测试一下");};
        NoParameterNoReturn noParameterNoReturn2 = () -> System.out.println("测试一下");
        NoParameterNoReturn noParameterNoReturn3 = () -> {
            System.out.println("测试1下");
            System.out.println("测试2下");
        };
    }
}
